package gupaoedu.prototype;

import java.io.*;
import java.util.List;

/**
 * gupaoedu.prototype
 * linwenjie
 * 2019-06-21
 * 模拟网上购买课程学习 购买者
 */
public class Person implements  Cloneable,Serializable{
    public String name;
    public List<Course> courses;//拥有的课程

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return this.deepClone();
    }

    public Object deepClone(){
        Person person = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);


            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            person =(Person)ois.readObject();


            oos.close();
            ois.close();
            bos.close();
            bis.close();



        } catch (Exception e) {
            e.printStackTrace();
        }
        return person;

    }

    public Object shallowClone(){
        Person copyPerson = new Person();
        copyPerson.setName(this.name);
        copyPerson.setCourses(this.courses);
        return copyPerson;
    }
}
